<template>
    <el-form :model="editForm">
        <el-form-item label="orignPassword">
            <el-input v-model="editForm.orignPassword" type="password"></el-input>
            <el-button @click="checkOrignPassword">请输入原始密码</el-button>
        </el-form-item>
        <el-form-item label="password">
            <el-input :disabled="isChecked" v-model="editForm.password" type="password"></el-input>
        </el-form-item>
        <el-form-item label="username">
            <el-input :disabled="isChecked" v-model="editForm.username"></el-input>
        </el-form-item>
        <el-form-item label="name">
            <el-input :disabled="isChecked" v-model="editForm.name" />
        </el-form-item>
        <el-form-item label="gender">
            <el-radio-group :disabled="isChecked" v-model="editForm.gender">
                <el-radio label="男" size="small">
                    男
                </el-radio>
                <el-radio label="女" size="small">
                    女
                </el-radio>
            </el-radio-group>
        </el-form-item>
        <el-form-item label="age">
            <el-slider :disabled="isChecked" v-model="editForm.age" show-input />
        </el-form-item>
    </el-form>
    <el-button type="success" @click="studentSubmit">提交</el-button>
</template>

<script setup>
import { ElMessage, ElButton,ElForm } from "element-plus";
import { reactive, ref, watch } from "vue";
import { checkPassword, getTeacherMessage, getMySelfIntroduce, updateMessage } from '../API/ApiMethod'
import { AES_Encrypt, AES_Decrypt } from "../crypto";
components: {
    ElButton,
    ElForm
}
const editForm = reactive({
    orignPassword: '',
    username: '',
    password: '',
    name: '',
    age: 0,
    gender: ''
})
const watchData = reactive({
    username: '',
    password: '',
    name: '',
    age: 0,
    gender: ''
})
let type = sessionStorage.getItem('type')
let isChecked = ref(true)
const checkOrignPassword = () => {
    if (editForm.orignPassword != '') {
        editForm.orignPassword = AES_Encrypt(editForm.orignPassword)
        checkPassword(editForm.orignPassword).then(res => {
            editForm.orignPassword = ''
            if (res.code == 10000) {
                isChecked.value = false
                ElMessage({
                    type: 'success',
                    message: "可以修改信息"
                })
                if (type == 'teacher') {
                    getTeacherMessage().then(res => {
                        console.log(res)
                        if (res.code == 10000) {
                            editForm.name = res.data.t_name
                            editForm.age = res.data.t_age
                            editForm.gender = res.data.t_gender
                            editForm.username = res.data.username
                            editForm.password = res.data.password
                            watchData.name = res.data.t_name
                            watchData.age = res.data.t_age
                            watchData.gender = res.data.t_gender
                            watchData.username = res.data.username
                            watchData.password = res.data.password
                        }
                    })
                } else if (type == 'student') {
                    getMySelfIntroduce().then(res => {
                        editForm.name = res.data.s_name
                        editForm.age = res.data.s_age
                        editForm.gender = res.data.s_gender
                        editForm.username = res.data.username
                        editForm.password = res.data.password
                    })
                }
            } else {
                editForm.orignPassword = ""
                ElMessage({
                    type: 'danger',
                    message: '密码错误，仔细想想吧'
                })
            }
        })
    } else {
        ElMessage({
            type: 'error',
            message: '密码不能为空'
        })
    }
}

const studentSubmit = () => {
    if (editForm.username == watchData.username) {
        editForm.username = ''
    }
    if (editForm.password == watchData.password) {
        editForm.password = ''
    } else if (editForm.password != watchData.password) {
        editForm.password = AES_Encrypt(editForm.password)
    }
    if (editForm.name == watchData.name) {
        editForm.name = ''
    }
    if (editForm.age == watchData.age) {
        editForm.age = ''
    }
    if (editForm.gender == watchData.gender) {
        editForm.gender = ''
    }
    updateMessage(editForm.password, editForm.name, editForm.gender, editForm.age, editForm.username).then(res => {
        if (res.code == 10000) {
            editForm.name = res.data.t_name
            editForm.age = res.data.t_age
            editForm.gender = res.data.t_gender
            editForm.username = res.data.username
            editForm.password = res.data.password
            watchData.name = res.data.t_name
            watchData.age = res.data.t_age
            watchData.gender = res.data.t_gender
            watchData.username = res.data.username
            watchData.password = res.data.password
            ElMessage({
                type:'success',
                message:'修改信息成功'
            })
        }else{
            ElMessage({
                message:'修改失败',
                type:'danger'
            })
        }
    })
}
</script>

<style>

</style>